#import "ResultUIView.h"
#import "StringUtil.h"
#import "FontFactory.h"
#import "HttpRequestParamEnginner.h"

@implementation CResultUIView

@synthesize iViewType;
@synthesize iLucktyType;
@synthesize iAllUrl;
- (id)initWithFrame:(CGRect)frame;
{
	if(self=[super initWithFrame:frame])
	{	
		return self;
	}
	return self;
}
-(void)requestHttp
{
	CHttpCommucationEngineer *engineer=[CHttpCommucationEngineer GetIntance];
	iConnection=[engineer HttpRequest:iAllUrl tag_Method:HTTP_GET tag_Data:nil tag_ControlId:self tag_Flag:0 tag_Pos:0];
}

-(void)HttpTextResponse:(HttpTextResponseData*)data
{
	int length;
	unsigned char *xmlString;
	length=data->length;
	xmlString=data->data;
	[self ParserXml:xmlString];
	free(xmlString);
}
-(void)ParserXml:(unsigned char *)xmlString
{
	const char *text;
	const char *attr;
	NSString *textString;
	TiXmlDocument *doc=new TiXmlDocument;
	doc->Parse((const char*)xmlString);
	TiXmlElement* RootElement = doc->RootElement();
	TiXmlElement *item = RootElement->FirstChildElement("Result");
	TiXmlElement *dataItem;
	CGRect rect;
	UIFont *bigFont;
	UIFont *littleFont;
	int yPos;
	CHttpRequestParamEnginner *enginner=[CHttpRequestParamEnginner GetIntance];
	int labelWidth=150;
	int viewWidth=self.frame.size.width;
	rect.origin.x=40;
	rect.origin.y=30;
	rect.size.width=labelWidth;
	rect.size.height=20;
	UILabel *noteLabel=[[UILabel alloc] initWithFrame:rect];
	[noteLabel setBackgroundColor:[UIColor clearColor]];
	bigFont=[CFontFactory CreateFontBySizeAndWeight:15 weightTag:1];
	[noteLabel setFont:bigFont];
	[noteLabel setText:@"基本信息"];
	[noteLabel setTextColor:[UIColor whiteColor]];
	[self addSubview:noteLabel];
	[noteLabel release];
	rect.origin.x=50;
	rect.origin.y=60;
	rect.size.width=labelWidth;
	rect.size.height=20;
	noteLabel=[[UILabel alloc] initWithFrame:rect];
	[noteLabel setBackgroundColor:[UIColor clearColor]];
	littleFont=[CFontFactory CreateFontBySizeAndWeight:13 weightTag:1];
	[noteLabel setFont:littleFont];
	NSMutableString *string=[[NSMutableString alloc]init];
	[string appendString:enginner->iParam.iBirthday];
	if(iLucktyType==LucktyTypeFateSY||iLucktyType==LucktyTypeFateJT||iLucktyType==LucktyTypeFateAQ||iLucktyType==LucktyTypeFateCY||iLucktyType==LucktyTypeFateHY)
	{
		if([enginner->iParam.iSex compare:@"1"]==0)
		{
			[string appendFormat:@"    %@",@"男"];
		}else {
			[string appendFormat:@"    %@",@"女"];
		}
		
	}
	[noteLabel setText:string];
	[string release];
	[noteLabel setTextColor:[UIColor whiteColor]];
	[self addSubview:noteLabel];
	[noteLabel release];
	rect.origin.x=40;
	rect.origin.y=90;
	rect.size.width=labelWidth;
	rect.size.height=20;
	noteLabel=[[UILabel alloc] initWithFrame:rect];
	[noteLabel setBackgroundColor:[UIColor clearColor]];
	[noteLabel setFont:bigFont];
	[noteLabel setText:@"出生地点"];
	[noteLabel setTextColor:[UIColor whiteColor]];
	[self addSubview:noteLabel];
	[noteLabel release];
	rect.origin.x=50;
	rect.origin.y=120;
	rect.size.width=labelWidth;
	rect.size.height=20;
	noteLabel=[[UILabel alloc] initWithFrame:rect];
	[noteLabel setBackgroundColor:[UIColor clearColor]];
	[noteLabel setFont:littleFont];
	NSString *addr=[NSString stringWithFormat:@"%@%@%@",enginner->iParam.iProvince,enginner->iParam.iCity,enginner->iParam.iZone]; 
	[noteLabel setText:addr];
	[noteLabel setTextColor:[UIColor whiteColor]];
	[self addSubview:noteLabel];
	[noteLabel release];
	rect.origin.x=40;
	rect.origin.y=150;
	rect.size.width=labelWidth;
	rect.size.height=20;
	noteLabel=[[UILabel alloc] initWithFrame:rect];
	[noteLabel setBackgroundColor:[UIColor clearColor]];
	[noteLabel setFont:bigFont];
	[noteLabel setText:@"分析结果"];
	[noteLabel setTextColor:[UIColor whiteColor]];
	[self addSubview:noteLabel];
	[noteLabel release];	
	yPos=180;
	if(item)
	{
		dataItem=item->FirstChildElement("item");
		if(dataItem)
		{
			while(dataItem)
			{
			attr=dataItem->Attribute("value");
			if(attr)
			{
				rect.origin.x=40;
				rect.origin.y=yPos;
				yPos+=20;
				rect.size.width=labelWidth;
				rect.size.height=20;
				textString=[CStringUtil TransformCharToNSString:(char*)attr];
				noteLabel=[[UILabel alloc] initWithFrame:rect];
				[noteLabel setBackgroundColor:[UIColor clearColor]];
				[noteLabel setFont:bigFont];
				[noteLabel setText:textString];
				[noteLabel setTextColor:[UIColor whiteColor]];
				[self addSubview:noteLabel];
				[noteLabel release];	
				
			}
			text=dataItem->GetText();
			if(text)
			{
				textString=[CStringUtil TransformCharToNSString:(char*)text];
				textString=[self ReplaceData:textString];
				noteLabel=[self CreateLabel:0 y:yPos text:textString];
				yPos+=noteLabel.frame.size.height+5;
				[self addSubview:noteLabel];
				[noteLabel release];	
			}
			
			dataItem=dataItem->NextSiblingElement();
			}
		}
		else
		{
			text=item->GetText();
			if(text)
			{
				textString=[CStringUtil TransformCharToNSString:(char*)text];
				noteLabel=[self CreateLabel:0 y:yPos text:textString];
				yPos+=noteLabel.frame.size.height+5;
				[self addSubview:noteLabel];
				[noteLabel release];	
			}
		}
	}
	[self setContentSize:CGSizeMake(320, yPos)];
	delete doc;
}
-(NSString*)ReplaceData:(NSString*) text
{
	text=[text stringByReplacingOccurrencesOfRegex:@"此项影响度\\d+%" withString:@""];
	text=[text stringByReplacingOccurrencesOfRegex:@"。，" withString:@"。"];
	text=[text stringByReplacingOccurrencesOfRegex:@".此领域在你人生所有领域重要度比重.*?\\d+%." withString:@""];
	text=[text stringByReplacingOccurrencesOfRegex:@"。，" withString:@"。"];
	text=[text stringByReplacingOccurrencesOfRegex:@".此领域在你人生领域中的重要度.*?\\d{1,}" withString:@""];
	text=[text stringByReplacingOccurrencesOfRegex:@"，，" withString:@"，"];
	text=[text stringByReplacingOccurrencesOfRegex:@"。，，" withString:@"。"];
	text=[text stringByReplacingOccurrencesOfRegex:@"。，" withString:@"。"];
	
	return text;
}
-(UILabel*)CreateLabel:(int)aX y:(int)aY text:(NSString*)aText
{
	UILabel *label=[[UILabel alloc] initWithFrame:CGRectZero];
	if(aText)
	{
		[label setNumberOfLines:0];
		UIFont *font = [CFontFactory CreateFontBySizeAndWeight:13 weightTag:1];
		CGSize size = CGSizeMake(320,2000);
		CGSize labelsize = [aText sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
		[label setFrame:CGRectMake(aX,aY, labelsize.width, labelsize.height)];
		label.font=font;
		[label setBackgroundColor:[UIColor clearColor]];
		[label setTextColor:[UIColor whiteColor]];
		label.text=aText;
	}
	return label;
}
-(void)dealloc
{
	if(iAllUrl)
	{
		[iAllUrl release];
	}
	[super dealloc];
}


@end

